<script setup>
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import { artDelService, artGetListService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'

//自定义params对象，存储下拉选择
const params = ref({
  pagenum: 1, //当前页
  pagesize: 5, //每页条数
  cate_id: '',
  state: ''
})
//存储当前总的文章条数
const total = ref(0)

// 存储文章列表
const articleList = ref([])

//添加文章
const articleEditRef = ref()
const onAddArticle = () => {
  articleEditRef.value.open({})
}

//编辑文章列表
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

// 将文章发布/存为草稿
const onSuccess = (state) => {
  // 如果是添加文章 则追加到最后 渲染需要回到最后一页
  if (state === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
  //如果是编辑 则渲染完还在当前页
}
//删除文章列表
const onDeleteArticle = async (row) => {
  // console.log(row)
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage.success('删除成功')
  getArticleList()
}

//定义变量 管理loading状态
const loading = ref(false)

//获取文章列表函数
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}

//调用函数
getArticleList()

//size变化,调用函数
const onSizeChange = (size) => {
  //只要每页条数变化，则重新从第一页开始渲染
  params.value.pagenum = 1
  params.value.pagesize = size
  //重新渲染
  getArticleList()
}

//当前页变化，调用函数
const onCurrentChange = (page) => {
  params.value.pagenum = page
  // 页码变化，对应渲染
  getArticleList()
}

//搜索
const onSearch = () => {
  // 从第一页开始重新渲染
  params.value.pagenum = 1
  getArticleList()
}
//重置
const onReset = () => {
  // 将分类和状态全部重新重新为0，从第一页开始，重新渲染
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
</script>

<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>

    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <!-- label 展示给用户看的，value 收集起来提交给后台 -->
        <channel-select v-model="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <!-- 这里后台标记发布状态，就是通过中文标记的，已发布/草稿 -->
        <el-select style="width: 200px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="articleList" style="width: 100%" v-loading="loading">
      <el-table-column label="文章标题" width="400">
        <!-- 利用作用域插槽 row（类似于item） 可以获取当前行的数据 =>v-for 遍历 用于自定义结构-->
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <!-- prop用于获取固定数据显示在页面 -->
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }"> {{ formatTime(row.pub_date) }} </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <!-- 分页实现 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[3, 5, 7, 9]"
      layout="jumper,total, sizes, prev, pager, next, "
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- 弹窗 -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
  </page-container>
</template>
<style lang="scss" scoped></style>
